

#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <memory>

//test move
void test_case1() {

    std::string str = "Hello";
    std::vector<std::string> v;
    //调用常规的拷贝构造函数，新建字符数组，拷贝数据
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";
    //调用移动构造函数，掏空str，掏空后，最好不要使用str
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";
    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";

}

//test swap
void test_case2() {
    std::string str1 = "hello";
    std::string str2 = "world";
    std::cout << "before: str1 " << str1 << "  str2 " << str2 << std::endl;
    std::swap(str1, str2);
    std::cout << "after: str1 " << str1 << "  str2 " << str2 << std::endl;
}

//test move 2
void test_case3() {

    std::unique_ptr<int> ptr1(new int(1));
    std::cout << "*ptr1 " << *ptr1 << std::endl;
    std::unique_ptr<int> ptr2 = std::move(ptr1);

    ptr2 = std::move(ptr1);

    if (ptr1 == nullptr) {
        std::cout << "ptr1 does not exist now." << std::endl;
    }
}

int main() {

    //test_case1();
    //test_case2();
    test_case3();
    return 0;
}